package com.cfc.stage1;

import org.junit.Test;

/**
 * @description:
 * @author: yangxuan10
 * @create: 2024-04-12 09:14
 **/
public class LengthOfLongestSubstring {

    @Test
    public void test1() {
        String str = "tmmzuxt";
//        str = "wobgrovw";
        int length = lengthOfLongestSubstring(str);
        System.out.println(length);
    }

    public int lengthOfLongestSubstring(String str) {
        if (str == null || str.length() ==0) {
            return 0;
        }
        char[] toCharArray = str.toCharArray();
        int length = toCharArray.length;
        int startIndex = 0;
        int endIndex = 1;
        int maxLength = 1;
        int finalMaxLength = 1;
        while (true) {
            if (endIndex == length || finalMaxLength >= (length - startIndex)) {
                break;
            }
            //tmmzuxt
            if (toCharArray[startIndex] != toCharArray[endIndex]) {
                boolean flag = true;
                int tmp = startIndex;
                while (true) {
                    if (tmp == endIndex-1) {
                        break;
                    }
                    tmp++;
                    if (toCharArray[tmp] == toCharArray[endIndex]) {
                        startIndex = tmp+1;
                        tmp = endIndex-1;
                        if (startIndex == endIndex) {
                            endIndex++;
                        }
                        flag= false;
                    }
                }
                if (flag) {
                    maxLength = endIndex - startIndex + 1;
                    endIndex++;
                }
                finalMaxLength = Math.max(finalMaxLength, maxLength);
            } else {
                finalMaxLength = Math.max(finalMaxLength, maxLength);
                maxLength = 1;
                startIndex++;
                endIndex = startIndex + 1;
            }
        }
        return finalMaxLength;
    }

}